﻿#pragma once

#include "Misc/EngineEditorMacros.h"
#include "Framework/EditorViewModelSubsystem.h"
#include "Misc/EngineEditorMacros.h"
#include "Window/EditorWindowModel.h"
#include "Window/EditorWindowViewModel.h"
#include "EditorContextMenuModel.h"
#include "Event/MouseEvent.h"
#include "EditorWindowGroupViewModel.m.h"

class REditorWindowGroupModel;
class REditorWindowGroupView;

class REditorWindowModel;
class REditorWindowViewModel;
class REditorWindowView;

class RWidget;
class REditorToolTipWindowViewModel;
class REditorContextMenuViewModel;

class REditorPageViewModel;

//view model of a window group
//also the root object of all view model
RCLASS(ExtraHeader=["Window/EditorWindowViewModel.h" , "Window/EditorWindowModel.h"])
class ENGINEEDITOR_API REditorWindowGroupViewModel : public REditorViewModelSubsystem
{
    ROBJECT

public:
    virtual void Initialize() override;
    virtual void Deinitialize() override;

    static REditorWindowGroupViewModel* Get();

    void RegisterWindow(TSharedObjectPtr<REditorWindowViewModel> WindowViewModel);
    void UnregisterWindow(TSharedObjectPtr<REditorWindowViewModel> WindowViewModel);
    TMultiCastDelegate<TSharedObjectPtr<REditorWindowViewModel>> OnWindowRegistered;
    TMultiCastDelegate<TSharedObjectPtr<REditorWindowViewModel>> OnWindowUnregistered;
    TVector<TSharedObjectPtr<REditorWindowViewModel>> GetWindows() const;
    
    void CloseWindow(TSharedObjectPtr<REditorWindowViewModel> WindowViewModel);

    TSharedObjectPtr<REditorPageViewModel> GetActivedPageViewModel() const;
    
    //在当前激活页面执行剪切操作
    //返回是否消耗了剪切操作
    bool CutInActivedPage() ;
    //在当前激活页面执行复制操作
    //返回是否消耗了复制操作
    bool CopyInActivedPage() ;
    //在当前激活页面执行粘贴操作
    //返回是否消耗了粘贴操作
    bool PasteInActivedPage() ;

    //在当前激活页面打开文本查找对话框
    //返回是否消耗了打开操作
    //仅文本编辑器支持
    bool OpenFindTextDialogInActivedPage();
    //在当前激活页面打开文本替换对话框
    //返回是否消耗了打开操作
    //仅文本编辑器支持
    bool OpenReplaceTextDialogInActivedPage();

    bool SaveActivedPage();
    bool UndoActivedPage();
    bool CanUndoActivedPage();
    bool RedoActivedPage();
    bool CanRedoActivedPage();

protected:
    TSharedObjectPtr<REditorWindowViewModel> CreateWindowViewModel(
        TSharedObjectPtr<REditorWindowModel> WindowModel 
    );

    RFIELD()
    TVector<TSharedObjectPtr<REditorWindowViewModel>> Windows;

//Tooltip related:
public:
    //try show a tooltip
    //will delay a while before show
    void ShowToolyipLater(
        const TWidgetAttribute<CString>& InTooltipContent
        ,TSharedObjectPtr<RWidget> InAroundWidget
    );
    void HideTooltip();

    TSharedObjectPtr<REditorToolTipWindowViewModel> GetTooltip() const
    {
        return Tooltip;
    }
protected:

    TSharedObjectPtr<REditorToolTipWindowViewModel> Tooltip;

//context menu related:
public:
    void ShowContextMenu(
        const TVector<SEditorContextMenuLine>& InRootItems
        ,const SIntPoint& InScreenPosition
        ,float InRootMenuWidth=350
        ,TDelegate<void> InOnContextMenuClosed=nullptr
    );

    void ShowContextMenu(
        const TVector<SEditorContextMenuLine>& InRootItems
        ,TSharedObjectPtr<RWidget> InAroundWidget
        ,float InRootMenuWidth=350
    );

    void HideContextMenu();

    bool IsContextMenuVisible() const;

    TSharedObjectPtr<REditorContextMenuViewModel> GetContextMenu() const
    {
        return ContextMenu;
    }

protected:
    TSharedObjectPtr<REditorContextMenuViewModel> ContextMenu;

};